/*
 * To change this template, choose Tools | Templates and open the template in the editor.
 */
package video;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.MediaException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.protocol.DataSource;
import javax.tv.xlet.XletStateChangeException;

import org.havi.ui.HContainer;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HSceneTemplate;
import org.havi.ui.HScreen;
import org.havi.ui.HStaticText;
import org.havi.ui.event.HRcEvent;

/**
 *
 * @author root
 */
public class Canal02 extends HContainer implements ControllerListener, KeyListener {

    private HStaticText label;

    private HContainer container;

    private static final String VIDEO_URL_2 = "file://F:/TCC/media/LuckyDay3_1.avi";

    //private static final String VIDEO_URL_2 = "file://root/NetBeansProjects/JavaApplication1/build/classes/outh263pcm.avi";
    private HScene scene;

    private Player player;

    private Component comp;

    Canal01 canal01;

    int t = 1;

    public void startVideo() {
        System.out.println("-----------startVideo-----------: ");
        // first, create a MediaLocator for the video we want to play.  In a
        // normal MHP or OCAP implementation this would be created from a 
        // dvb:// or ocap:// URL respectively.

        //criascena();

        javax.media.MediaLocator locator;

        //Set our scene to be visible.  Later in the Xlet we will put the video 
        // in an HComponent, so we need a visible HScene in which to display it.
        System.out.println("fora de startScene...1: " + scene.getSceneTemplate());
        scene.setVisible(true);
        System.out.println("fora de startScene...2: " + scene.getSceneTemplate());
        //		while (true) {
        try {
            // Create a data source for our locator.  We could create the player
            // directly by calling Manager.createPlayer() with the MediaLocator if we
            // preferred - the effect is the same.
            locator = new javax.media.MediaLocator(VIDEO_URL_2);
            DataSource playingDataSource = Manager.createDataSource(locator);
            // Now create the player
            player = Manager.createPlayer(playingDataSource);

            // Add ourselves as a listener for events from the player
            //            teste =1;
            player.addControllerListener(this);

            // Start the player.  At this point, any video would be played in the 
            // background on a normal MHP or OCAP receiver.
            System.out.println("starting...");
            //try {
            //    Thread.sleep(3000);
            //} catch (Exception e) {
            //    e.printStackTrace();
            //}
            player.start();

            System.out.println("fora de startScene...3: " + scene.getSceneTemplate());

            System.out.println("sleeping...");
            //            try {
            //                Thread.sleep(3000);
            //            } catch (Exception e) {
            //                e.printStackTrace();
            //            }
            //            System.out.println("stopping video 1");
            addLabel();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (MediaException e) {
            e.printStackTrace();
        }
    }

    public void criascena() {

        HSceneFactory factory = HSceneFactory.getInstance();

        // Build a full-screen HSceneTemplate.
        HSceneTemplate hst = new HSceneTemplate();
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_DIMENSION, new org.havi.ui.HScreenDimension(1, 1), HSceneTemplate.REQUIRED);
        hst.setPreference(HSceneTemplate.SCENE_SCREEN_LOCATION, new org.havi.ui.HScreenPoint(0, 0), HSceneTemplate.REQUIRED);

        // Now get our HScene.
        //        scene = factory.getBestScene(hst);
        HSceneFactory hsceneFactory = HSceneFactory.getInstance();
        scene = hsceneFactory.getFullScreenScene(HScreen.getDefaultHScreen().getDefaultHGraphicsDevice());
        System.out.println("DENTRO DE CRIASCENE................: " + scene.toString());
        // If we can't get an HScene that meets our needs, die.
        if (scene == null) {
            try {
                throw new XletStateChangeException("Can't create HScene");
            } catch (XletStateChangeException ex) {
                ex.printStackTrace();
            }
        }

        // If we have got a suitable HScene, set our size to be the full size of
        // the HScene and add ourselves to it.
        Rectangle rect = scene.getBounds();
        scene.setBounds(rect);
        scene.setBounds(200, 10, 420, 276);
        scene.setVisible(false);

        scene.addKeyListener(this);
        scene.setVisible(true);
        scene.requestFocus();

        scene.add(this);
        System.out.println("DENTRO DE CRIASCENE.....FIM...........: " + rect);
    }

    public void chamaCanal1() {
        System.out.println("TESTE---------- CANAL 01 entreouuuuuuuu !!!!!!!!!!!!!!!as a video component");
        canal01 = new Canal01();
        canal01.setScene(scene, this.container);
        canal01.startVideo();
        //scene.addKeyListener(this);
    }

    public void addLabel() {
        this.label = new HStaticText("Exemplo de TEXTO!!!");
        this.label.setFont(new Font("Verdana", Font.PLAIN, 26));//font Tiresias
        this.label.setBounds(10, 200, 720, 50);
        this.label.setForeground(Color.BLACK);
        this.label.setBackground(new Color(255, 255, 255, 200));

        Component compIn2;

        // Adiciona o Label a Scene
        scene.setBounds(0, 0, 720, 576);
        scene.setVisible(false);
        //scene.add(label); 
        container.add(label, 0);
        scene.validate();
        scene.setVisible(true);
        scene.repaint();

        //startVideo();
    }

    public void chamaStartVideo() {
        scene.setBounds(0, 0, 720, 576);
        scene.setVisible(false);
        //scene.add(label);
        //container.add(label,0);
        scene.validate();
        scene.setVisible(true);
        scene.repaint();
        startVideo();
    }

    public void controllerUpdate(ControllerEvent event) {
        System.out.println("----------.........: " + event);
        if (event instanceof RealizeCompleteEvent) {
            // Try to display the video in a window (admittedly, a full-screen window)
            Component compIn;
            // Calling getVisualComponent() will convert the player from a
            // 'background player' (which plays its video in the video plane)
            // to a 'component player' (which plays its video in an AWT component).
            // This is a one-way operation, so the only way to go back is to
            // destroy the current player and create a new one.  If the operation 
            // fails, then the player will remain as a background player.
            if ((compIn = player.getVisualComponent()) != null) {

                //scene.add(compIn);
                compIn.setBounds(200, 10, 420, 276);
                container.add(compIn, t);
                this.comp = compIn;
                System.out.println("Added " + compIn + " as a video component");
                // Set the size and position (full screen, placed in the top left corner)
                // and draw it.
                //                comp.setSize(720, 576);
                //                setSize(720, 576);
                //compIn.setSize(420, 276);
                //scene.setSize(420, 276);
                //scene.setBounds(200, 10, 420, 276);
                //scene.setBounds(0, 0, 720, 276);
                scene.validate();
                scene.repaint();

            }

        } else if (event instanceof EndOfMediaEvent) {
            // We've reached the end of the media; rewind and 
            // start over.

            // Note that setMediaTime() will NOT work in this way on a real MHP
            // implementation.  The concept of 'media time' has no real meaning in a
            // broadcast environment, and so xlets can only set the media time of
            // audio clips that are played from a file.
            player.stop();
            scene.remove(this.comp);
            scene.repaint();
            teste();
            //            player.setMediaTime(new Time(0));
            //            player.start();
        }
    }

    public void setScene(HScene pai, HContainer container) {
        this.scene = pai;
        container = this;
        this.container = container;
        //scene.add(container);

        this.container.setBounds(0, 0, 720, 576);

        scene.add(this.container);
        scene.setVisible(true);

        Graphics g;
        g = scene.getGraphics();
    }

    public void teste() {
        System.out.println("--------------CANAL02 CANAL02 ------------");
    }

    /*    @Override
        public void paint(Graphics g) {
            //desenha fundo da tela
            System.out.println("paint FF PAINT CANAL02 --- primeiro");
            
                //System.out.println("paint FF");
                g.setColor(new Color(200,0,0)); //Elegimos color negro para hacer el marco exterior
                g.fillRect(10, 400, 200, 50);
                //desenha imagens
               
                //desenha borda
                g.setFont(new Font("Verdana", Font.PLAIN, 26));
                
                g.drawString("&&&&&&&&&&&&&&&&&&&&&", 10, 400);


                super.paint(g);
            
        }
    */
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void keyPressed(KeyEvent e) {
        System.out.println("CANAL02 CANAL02 CANAL02 CANAL02 CANAL02");
        System.out.println("CANAL02---------CANAL02-----------------------");
        if (e.getKeyChar() == HRcEvent.VK_DOWN) {
            System.out.println("\npara cima o de dentro !!!!!!!!!!");
            player.stop();
            scene.remove(this.comp);
            scene.repaint();
            chamaCanal1();

        } else if ((e.getKeyCode() == 404) || (e.getKeyCode() == 405) || (e.getKeyCode() == 406)) {
            System.out.println("----------============outros botoes");
            addLabel();
        } else if (e.getKeyChar() == HRcEvent.VK_1) {
            System.out.println("\nbutao 01!!!!!!!!!!");
            addLabel();
            t = 1;

        }
    }

    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
